/*
 * TOP SECRET
 * Copyright 2006-2015 Transsion.com All right reserved. This software is the
 * confidential and proprietary information of Transsion.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.ng;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.biz.crawler.NewsCrawler;
import com.yunji.framework_template.biz.crawler.SourceType;
import com.yunji.framework_template.common.enumeration.CountryCode;
import com.yunji.framework_template.common.enumeration.NewsType;
import com.yunji.framework_template.common.util.NumberUtil;


/** 
 * ClassName:LegitNewsCrawler <br/> 
 * Date:     2018年12月17日 下午3:18:55 <br/> 
 * @author   fenglibin1982@163.com
 * @Blog     http://blog.csdn.net/fenglibing
 * @version  
 * @see       
 */
@Service
public class LegitNewsCrawler extends NewsCrawler {

    @Override
    public boolean isOkUrl(String url) {
        if(!url.endsWith(".html")) {
            return false;
        }
        URI uri;
        try {
            if (url == null || url.trim().length() == 0) {
                return false;
            }
            if (url.startsWith("mailto") || url.toLowerCase().startsWith("javascript")) {
                return false;
            }
            uri = new URI(url);
            String path = uri.getPath();
            String[] pathArr = path.split("/");
            if (pathArr.length < 2) {
                return false;
            }
            String path1 = pathArr[1];
            String[] path1Arr = path1.split("-");
            if(NumberUtil.isNumber(path1Arr[0])) {
                return true;
            }
        } catch (URISyntaxException e) {
        }
        return false;
    }

    @Override
    public ContentImageHandler getContentImageHandler() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<SourceType> getSourceTypeList() {
        List<SourceType> sourceTypeList = new ArrayList<SourceType>();
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/latest/").newsType(NewsType.NEWS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/premier-league/").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("https://hausa.legit.ng/").newsType(NewsType.LIFESTYLE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/politics/").newsType(NewsType.FINANCE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/gossip/").newsType(NewsType.FUN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/entertainment/").newsType(NewsType.ENTERTAINMENT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/ask_legit/").newsType(NewsType.ASKANDANSWER).build());
        sourceTypeList.add(SourceType.builder().url("https://www.legit.ng/sports/").newsType(NewsType.SPORT).build());
        return sourceTypeList;
    }

    @Override
    public Set<String> getCountryCodeSet() {
        Set<String> countryCodeSet = new HashSet<String>();
        countryCodeSet.add(CountryCode.NG.name());
        return countryCodeSet;
    }
}
